package com.whp.openapi.web.controller;

import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.entity.ApiMapping;
import com.whp.openapi.service.ApiMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author whp
 * @date 2021/11/26
 */
@RestController
@RequestMapping("/api_mapping")
public class ApiMappingController {
    
    @Autowired
    private ApiMappingService apiMappingService;

    /**
     * 路由管理：搜索、分页
     * @param page
     * @param limit
     * @param gatewayApiName
     * @param state
     * @return
     */
    @RequestMapping("/table")
    public ResultTable table(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             String gatewayApiName,
                             Integer state){
        return apiMappingService.table(page, limit, gatewayApiName, state);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/del")
    public ResponseResult delApiMapping(Integer[] ids){
        try {
            return apiMappingService.delApiMapping(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败");
    }

    /**
     * 添加
     * @param apiMapping
     * @return
     */
    @RequestMapping("/add")
    public ResponseResult addApiMapping(ApiMapping apiMapping){
        try {
            return apiMappingService.addApiMapping(apiMapping);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败");
    }

    /**
     * 编辑
     * @param apiMapping
     * @return
     */
    @RequestMapping("/update")
    public ResponseResult updateApiMapping(ApiMapping apiMapping){
        try {
            return apiMappingService.updateApiMapping(apiMapping);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败");
    }
}
